package com.xc.freeapp.account.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;

import com.alibaba.fastjson.JSONArray;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.JsonArray;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.entity.Vaccineperson;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.VaccinepersonService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.JsonUtil;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;
@Controller
@RequestMapping("/VaccinePerson")
@Api(value="vaccinePerson",description = "接种人")
public class VaccinePersonController extends AbstractController {
	@Autowired
	private VaccinepersonService vaccinepersonServiceImpl;
	

	/**
	 * 保存接种人信息
	 * 
	 * @param request
	 * @return
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "保存接种人信息")
	public ResponseBean saveVaccinePerson(
			@RequestBody @Valid Vaccineperson vaccineperson)
			throws BaseException {
		
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			Map<String, String> map = new HashMap<String, String>();
		
			String headpic = UploadUtil.uploads(JSONArray.parseArray(vaccineperson.getHeadpic(), String.class),authInfo.getAppIntId());
			vaccineperson.setHeadpic(headpic);
			vaccineperson.setUserid(Integer.valueOf(authInfo.getUserId()));
			vaccineperson.setDelflag("t");
			vaccinepersonServiceImpl.addVaccineperson(vaccineperson);
			return ResponseBean.getSuccess();
		} catch (BaseException e) {
			throw e;
		} catch (Exception e) {
			log.error(authInfo.getUserId() + "保存接种人信息失败" + e.getMessage());
			e.printStackTrace();
			throw new BaseException(Constant.GLOBAL_ERROR);
		}

	}
	@TokenAnnotation(required=true)
	@RequestMapping(value = "",method = RequestMethod.PUT)
	@ResponseBody
	@ApiOperation(value = "更新接种人信息")
	public ResponseBean updateVaccinePerson(
			@RequestBody Vaccineperson vaccineperson)
			throws BaseException {
		
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			Map<String, String> map = new HashMap<String, String>();
		
			String headpic = UploadUtil.uploads(JSONArray.parseArray(vaccineperson.getHeadpic(), String.class),authInfo.getAppIntId());
			vaccineperson.setHeadpic(headpic);
			vaccineperson.setUserid(Integer.valueOf(authInfo.getUserId()));
			vaccinepersonServiceImpl.updateVaccineperson(vaccineperson);
			return ResponseBean.getSuccess();
		} catch (BaseException e) {
			throw e;
		} catch (Exception e) {
			log.error(authInfo.getUserId() + "更新接种人信息失败" + e.getMessage());
			e.printStackTrace();
			throw new BaseException(Constant.GLOBAL_ERROR);
		}

	}
	/**
	 * 获取接种人信息
	 * 
	 * @param request
	 * @return
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取接种人信息")
	public List<Vaccineperson>  getVaccinePersonList()
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			Map<String, String> map = new HashMap<String, String>();
			
			map.put("userId", authInfo.getUserId());
			PageBounds pageBounds = new PageBounds(0, 100);
			List<Vaccineperson> vaccinepersons = vaccinepersonServiceImpl
					.getVaccineperson(map,pageBounds);
			for (Vaccineperson vaccineperson : vaccinepersons) {
				vaccineperson.setHeadpic(JSONArray.toJSONString(UploadUtil.pathstoList(vaccineperson.getHeadpic())));
			}
			return vaccinepersons;
		} catch (Exception e) {
			log.error(authInfo.getUserId() + "获取接种人信息失败");
			throw new BaseException(Constant.GLOBAL_ERROR);
		}

	}
	@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
	@TokenAnnotation(required=true)
	@ResponseBody
	@ApiOperation(value = "删除接种人信息")
	public ResponseBean delVaccinePerson(@PathVariable("id") Integer id)
			throws BaseException {
			vaccinepersonServiceImpl.delVaccineperson(id);
			return ResponseBean.getSuccess();
	}
	

}
